package cn.xny.order.service.impl;

import cn.xny.basic.util.LoginContext;
import cn.xny.basic.util.PageList;
import cn.xny.order.domain.OrderPetAcquisition;
import cn.xny.order.dto.OrderDto;
import cn.xny.order.mapper.OrderPetAcquisitionMapper;
import cn.xny.order.query.OrderPetAcquisitionQuery;
import cn.xny.order.service.OrderPetAcquisitionService;
import cn.xny.basic.service.impl.BaseServiceImpl;
import cn.xny.org.domain.Employee;
import cn.xny.org.domain.Shop;
import cn.xny.org.mapper.EmployeeMapper;
import cn.xny.org.mapper.ShopMapper;
import cn.xny.pet.domain.Pet;
import cn.xny.pet.domain.SearchMasterMsg;
import cn.xny.pet.mapper.PetMapper;
import cn.xny.pet.mapper.SearchMasterMsgMapper;
import cn.xny.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements OrderPetAcquisitionService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    public PageList<OrderPetAcquisition> pageQuery(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);

        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());


        if (employee.getShop_id()!=null){//店铺管理员or店长=店铺管理员
            Shop shop = shopMapper.loadByAll(employee.getShop_id());
            if (shop.getAdminId()!=employee.getId()){

                query.setEmpId(employee.getId());
            }
            query.setShopId(employee.getShop_id());


        }

        return super.pageQuery(query);
    }

    //取消订单
    @Override
    public void cancel(Long id) {
        //获取当前订单
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadByAll(id);
          //把shopid设置为空
          searchMasterMsgMapper.reject(order.getSearchMasterMsgId());
          //把状态设置为-1
           order.setState(-1);

          orderPetAcquisitionMapper.update(order);

    }

    //确认订单
    @Override
    public void confirm(OrderDto orderDto) {
       
        Long id = orderDto.getId();
        BigDecimal money = orderDto.getMoney();//最终成交价
        Integer paytype = orderDto.getPaytype();//支付方式

        //1.生成宠物对象
        OrderPetAcquisition odred = orderPetAcquisitionMapper.loadByAll(id);
        SearchMasterMsg msg = searchMasterMsgMapper.loadByAll(odred.getSearchMasterMsgId());
           Pet pet= createPet(msg,money);
           petMapper.save(pet);
       //2.修改订单状态，并且修改金额最终成交价
           odred.setState(1);
           odred.setPrice(money);
           odred.setPetId(pet.getId());
           orderPetAcquisitionMapper.update(odred);
       //修改寻主状态消息的状态
         msg.setState(2);
         searchMasterMsgMapper.update(msg);
    }

    private Pet createPet(SearchMasterMsg msg, BigDecimal money) {
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setCostprice(money);
        pet.setSaleprice(money);
        pet.setTypeId(msg.getPetType());
        pet.setResources(msg.getResources());
        pet.setShopId(msg.getShopId());
        //pet.setUserId(msg.getUserId());  //这个是领养人的id
        pet.setSearchMasterMsgId(msg.getId());


        return pet;
    }
}
